/*
 * Sycloud.com Inc.
 * Copyright (c) 2005-2066 All Rights Reserved.
 */
package com.insigma.business.qrcode.welfare._bff.impl;


import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.CareWelfareServiceOrganizationDTO;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationInsertRequest;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationSearchRequest;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationUpdateRequest;
import com.insigma.business.qrcode.welfare._bff.CareWelfareServiceOrganizationBFFProvider;
import com.insigma.business.qrcode.welfare._consumer.CareWelfareServiceOrganizationConsumer;
import com.insigma.business.qrcode.welfare._provider.CareWelfareServiceOrganizationProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 福利-服务机构服务提供实现类
 * @author juffett
 * @version $Id: CareWelfareServiceOrganizationBFFProviderImpl.java, v 1.0.0 2021-9-9 14:45:13 juffett Exp $
 */
@Slf4j
@Service
@Component("careWelfareServiceOrganizationBFFProvider")
public class CareWelfareServiceOrganizationBFFProviderImpl extends BaseProviderImpl implements CareWelfareServiceOrganizationBFFProvider {

    @Autowired
    private CareWelfareServiceOrganizationConsumer careWelfareServiceOrganizationConsumer;

    /**
     * @see CareWelfareServiceOrganizationProvider#searchCareWelfareServiceOrganization(CareWelfareServiceOrganizationSearchRequest)
     */
    @Override
    public BasePagingResponse<List<CareWelfareServiceOrganizationDTO>> searchCareWelfareServiceOrganization(final CareWelfareServiceOrganizationSearchRequest searchRequest) {

        final BasePagingResponse<List<CareWelfareServiceOrganizationDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)searchRequest.getType(), "商家类型不能为空");
            }

            @Override
            public void executeService() {
                SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
                if (sessionSoldier != null) {
                    searchRequest.setPublished(true);
                }
                searchRequest.setSortKeyValueJoint("createDatetime_desc");
                BasePagingResponse<List<CareWelfareServiceOrganizationDTO>> listBasePagingResponse = careWelfareServiceOrganizationConsumer.searchCareWelfareServiceOrganization(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_SEARCH;
            }
        });

        return basePagingResponse;
    }


    /**
    * @see CareWelfareServiceOrganizationProvider#getCareWelfareServiceOrganization(BaseGetRequest)
    */
    @Override
    public BaseResponse<CareWelfareServiceOrganizationDTO> getCareWelfareServiceOrganization(final BaseGetRequest baseGetRequest){

        final BaseResponse<CareWelfareServiceOrganizationDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<CareWelfareServiceOrganizationDTO> careWelfareServiceOrganizationResponse = careWelfareServiceOrganizationConsumer.getCareWelfareServiceOrganization(baseGetRequest);
                BeanUtil.copyProperties(careWelfareServiceOrganizationResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCareWelfareServiceOrganization(final CareWelfareServiceOrganizationInsertRequest insertRequest){
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CareWelfareServiceOrganization的请求参数不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildCreatorOfGov(insertRequest);
                BaseResponse<String> careWelfareServiceOrganizationResponse = careWelfareServiceOrganizationConsumer.insertCareWelfareServiceOrganization(insertRequest);
                BeanUtil.copyProperties(careWelfareServiceOrganizationResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCareWelfareServiceOrganization(final CareWelfareServiceOrganizationUpdateRequest updateRequest){
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> careWelfareServiceOrganizationResponse = careWelfareServiceOrganizationConsumer.updateCareWelfareServiceOrganization(updateRequest);
                BeanUtil.copyProperties(careWelfareServiceOrganizationResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCareWelfareServiceOrganization(final BaseDeleteRequest deleteRequest){
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> careWelfareServiceOrganizationResponse = careWelfareServiceOrganizationConsumer.deleteCareWelfareServiceOrganization(deleteRequest);
                BeanUtil.copyProperties(careWelfareServiceOrganizationResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> publishOrg(CareWelfareServiceOrganizationUpdateRequest request) {
        return careWelfareServiceOrganizationConsumer.publishOrg(request);
    }
}
